package com.diancan.modules.admin.admin;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diancan.common.Result;
import com.diancan.domain.Admin;
import com.diancan.domain.Orders;
import com.diancan.modules.admin.admin.admin.AdminService;
import com.diancan.modules.good.GoodService;
import com.diancan.modules.order.OrderService;
import com.diancan.modules.user.user.UserService;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Map;

@RestController
@RequestMapping("adminOrder")
@CrossOrigin
public class AdminOrderController {
    private final OrderService orderService;
    private final UserService userService;
    private final AdminService adminService;
    private final GoodService goodService;

    public AdminOrderController(OrderService orderService, UserService userService, AdminService adminService, GoodService goodService) {
        this.orderService = orderService;
        this.userService = userService;
        this.adminService = adminService;
        this.goodService = goodService;
    }

    // 获取全部订单
    @GetMapping("getOrders")
    public Result getOrders() {
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        ArrayList<Orders> ordersArrayList = (ArrayList<Orders>) orderService.list(wrapper);
        for (Orders o:ordersArrayList){
            o.setUser(userService.getById(o.getUserId()));
        }
        return Result.buildSuccessResult(ordersArrayList);
    }

    // 获取待付款订单
    @GetMapping("getOrdersDfk")
    public Result getOrdersDfk() {
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id").eq("state", "待付款");
        ArrayList<Orders> ordersArrayList = (ArrayList<Orders>) orderService.list(wrapper);
        for (Orders o:ordersArrayList){
            o.setUser(userService.getById(o.getUserId()));
        }
        return Result.buildSuccessResult(ordersArrayList);
    }

    // 获取已付款订单
    @GetMapping("getOrdersYfk")
    public Result getOrdersYfk() {
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id").eq("state", "已付款");
        ArrayList<Orders> ordersArrayList = (ArrayList<Orders>) orderService.list(wrapper);
        for (Orders o:ordersArrayList){
            o.setUser(userService.getById(o.getUserId()));
        }
        return Result.buildSuccessResult(ordersArrayList);
    }

    // 获取已完成订单
    @GetMapping("getOrdersYwc")
    public Result getOrdersYwc() {
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id").eq("state", "已完成");
        ArrayList<Orders> ordersArrayList = (ArrayList<Orders>) orderService.list(wrapper);
        for (Orders o:ordersArrayList){
            o.setUser(userService.getById(o.getUserId()));
        }
        return Result.buildSuccessResult(ordersArrayList);
    }

    // 获取已取消订单
    @GetMapping("getOrdersYqx")
    public Result getOrdersYqx() {
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id").eq("state", "已取消");
        ArrayList<Orders> ordersArrayList = (ArrayList<Orders>) orderService.list(wrapper);
        for (Orders o:ordersArrayList){
            o.setUser(userService.getById(o.getUserId()));
        }
        return Result.buildSuccessResult(ordersArrayList);
    }

    // 完成订单
    @GetMapping("wcdd")
    public Result wcdd(String orderId) {
        if (orderId == null) {
            return Result.buildFailureResult("订单编号为空");
        }
        Orders order = orderService.getById(orderId);
        if (order == null) {
            return Result.buildFailureResult("订单不存在");
        }
        order.setState("已完成");
        return Result.buildSuccessResult("操作成功", orderService.updateById(order));
    }

    // 取消订单
    @GetMapping("qxdd")
    public Result qxdd(String orderId) {
        if (orderId == null) {
            return Result.buildFailureResult("订单编号为空");
        }
        Orders order = orderService.getById(orderId);
        if (order == null) {
            return Result.buildFailureResult("订单不存在");
        }
        order.setState("已取消");
        return Result.buildSuccessResult("操作成功", orderService.updateById(order));
    }

    // 删除订单
    @GetMapping("scdd")
    public Result scdd(String orderId) {
        if (orderId == null) {
            return Result.buildFailureResult("订单编号为空");
        }
        Orders order = orderService.getById(orderId);
        if (order == null) {
            return Result.buildFailureResult("订单不存在");
        }
        return Result.buildSuccessResult("操作成功", orderService.removeById(orderId));
    }

    // 查询订单
    @GetMapping("searchOrder")
    public Result searchOrder(String key){
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        ArrayList<Orders> ordersArrayList = new ArrayList<>();
        if(key == null || "".equals(key)){
            wrapper.orderByDesc("id");
        }
        else {
            wrapper.like("id",key).or().like("user_id",key).or().eq("table_id",key).orderByDesc("id");
        }
        ordersArrayList = (ArrayList<Orders>) orderService.list(wrapper);
        for (Orders o:ordersArrayList){
            o.setUser(userService.getById(o.getUserId()));
        }
        return Result.buildSuccessResult(ordersArrayList);
    }
}
